package cn.com.sueframe.tag;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * //���ҳ�洫�ݹ�4�����ݶ�ȡ��Դ�ļ����������
 * @author anler
 * Create Jul 7, 2012
 */
public class National extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2590011054130509587L;
	private String key;
	private String country;
	private PageContext pageContext;
	private JspWriter out;
	private Map<String,Object> nationalMap;
	
	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		out = pageContext.getOut();
		Object tempCountryObject = pageContext.findAttribute("ceswich");
		if(tempCountryObject == null){
			tempCountryObject = "";
		}
		String tempCountry = Locale.getDefault().getCountry();
		country = String.valueOf(tempCountryObject);
		country = country == null || "".equals(country)? tempCountry : country;
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		Object valueObject = getValueByKey();
		if(valueObject == null){
			valueObject = "";
		}
		String value = String.valueOf(valueObject); 
		try {
			out.write(value);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	/**
	 * ��ݴ��ݹ�4��ֵ�õ���ʻ��ļ��е�ֵ
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private String getValueByKey(){
		if("CN".equals(country)){
			nationalMap = (Map<String, Object>)pageContext.findAttribute("cnMap");
		}else{
			nationalMap = (Map<String, Object>)pageContext.findAttribute("enMap");
		}
		String value = String.valueOf(nationalMap.get(key));
		value = value == null || "null".equals(value) || "".equals(value)? key : value;
		return value;
	}

	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public PageContext getPageContext() {
		return pageContext;
	}

	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
	}
}
